package com.huawei.petapp2.impl;


import android.text.TextUtils;

import com.huawei.petapp2.activity.widget.App;
import com.huawei.petapp2.activity.widget.BaseActivity;
import com.huawei.petapp2.entity.ShopDetailBean;
import com.huawei.petapp2.utils.ACache;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 作者： njb
 * 时间： 2018/6/8 0008-下午 4:26
 * 描述： 购物车 相关实现
 * 来源：
 */


public class CartImpl {
    private static final String CART_CACHE = "CART_CACHE";


    /**
     * 获取缓存的对象
     *
     * @return
     */
    private static CartImplModel getCartModel() {
        ACache cache = ACache.get(BaseActivity.getContext());
        CartImplModel implModel = (CartImplModel) cache.getAsObject(CART_CACHE);
        return implModel;
    }

    /**
     * 保存
     *
     * @param model
     */
    private static void saveCartModel(CartImplModel model) {
        if (model != null) {
            model.setLastEditTime(System.currentTimeMillis());
        }
        ACache cache = ACache.get(App.getContext());
        cache.put(CART_CACHE, model);
    }

    /**
     * 获取购物车 商品列表
     *
     * @return
     */
    public static List<ShopDetailBean> getCartList() {
        CartImplModel implModel = getCartModel();
        if (implModel == null || implModel.getGoodsMap() == null) {
            return new ArrayList<>();
        }

        String userId = UserImpl.getUserId();
        if (!TextUtils.isEmpty(userId)) {
            HashMap<String, ShopDetailBean> detailModelHashMap = implModel.getGoodsMap().get(userId);
            if (detailModelHashMap == null) {
                return new ArrayList();
            }

            return new ArrayList(detailModelHashMap.values());
        }

        return new ArrayList();

    }

    /**
     * 获取购物车商品数量
     *
     * @return
     */
    public static int getCartSize() {
        CartImplModel implModel = getCartModel();
        if (implModel == null || implModel.getGoodsMap() == null) {
            return 0;
        }

        int count = 0;

        String userId = UserImpl.getUserId();

        if (TextUtils.isEmpty(userId)) {
            return count;
        }
        HashMap<String, ShopDetailBean> detailModelHashMap = implModel.getGoodsMap().get(userId);

        if (detailModelHashMap == null) {
            return count;
        }

//        for (ShopDetailBean model : detailModelHashMap.values()) {
////            if (model.isSelect()) {
//            count = count + model.getGood_count();
////            }
//        }

        return detailModelHashMap.size();
    }

    /**
     * 获取购物车商品是否全选
     *
     * @return
     */
    public static boolean getCartIsSelectAll() {
        CartImplModel implModel = getCartModel();
        if (implModel == null || implModel.getGoodsMap() == null) {
            return false;
        }


        String userId = UserImpl.getUserId();

        if (TextUtils.isEmpty(userId)) {
            return false;
        }
        HashMap<String, ShopDetailBean> detailModelHashMap = implModel.getGoodsMap().get(userId);

        if (detailModelHashMap == null) {
            return false;
        }
        if (detailModelHashMap.isEmpty()) {

            return false;
        }

        for (ShopDetailBean model : detailModelHashMap.values()) {
            if (!model.isSelect()) {
                return false;
            }
        }


        return true;
    }

    /**
     * 购物车商品是否有选中的
     *
     * @return
     */
    public static boolean getCartHasSelect() {
        CartImplModel implModel = getCartModel();
        if (implModel == null || implModel.getGoodsMap() == null) {
            return false;
        }


        String userId = UserImpl.getUserId();

        if (TextUtils.isEmpty(userId)) {
            return false;
        }
        HashMap<String, ShopDetailBean> detailModelHashMap = implModel.getGoodsMap().get(userId);

        if (detailModelHashMap == null) {
            return false;
        }
        if (detailModelHashMap.isEmpty()) {
            return false;
        }

        for (ShopDetailBean model : detailModelHashMap.values()) {
            if (model.isSelect()) {
                return true;
            }
        }


        return false;
    }


    /**
     * 加入购物车
     *
     * @param model
     */
    public static void addCart(ShopDetailBean model) {
        addCart(model, 1);
    }

    /**
     * 加入购物车
     *
     * @param model
     */
    public static void addCart(ShopDetailBean model, int count) {
        if (model == null) {
            return;
        }
        String userId = UserImpl.getUserId();
        if (TextUtils.isEmpty(userId)) {
            return;
        }

        CartImplModel implModel = getCartModel();
        if (implModel == null) {
            //第一次缓存
            implModel = new CartImplModel();

            HashMap<String, HashMap<String, ShopDetailBean>> goodsMap = new HashMap<>();

            HashMap<String, ShopDetailBean> map = new HashMap<>();
            model.setGood_count(count);
            map.put(model.getGoods_id(), model);

            goodsMap.put(userId, map);

            implModel.setGoodsMap(goodsMap);

        } else {
            if (implModel.getGoodsMap() != null) {

                HashMap<String, ShopDetailBean> detailModelHashMap = implModel.getGoodsMap().get(userId);
                if (detailModelHashMap == null) {
                    detailModelHashMap = new HashMap<>();
                }

                if (detailModelHashMap.containsKey(model.getGoods_id())) {
                    //如果有，数量加1
                    ShopDetailBean old = detailModelHashMap.get(model.getGoods_id());
                    old.setGood_count(old.getGood_count() + count);
                } else {
                    //如果没有，加入
                    model.setGood_count(count);
                    detailModelHashMap.put(model.getGoods_id(), model);
                }
                implModel.getGoodsMap().put(userId, detailModelHashMap);
            }
        }

        saveCartModel(implModel);
    }

    /**
     * 移除购物车
     *
     * @param model
     */
    public static void minusCart(ShopDetailBean model) {
        if (model == null) {
            return;
        }
        CartImplModel implModel = getCartModel();
        if (implModel == null) {
            return;
        }
        if (implModel.getGoodsMap() == null) {
            return;
        }
        String userId = UserImpl.getUserId();
        if (TextUtils.isEmpty(userId)) {
            return;
        }

        HashMap<String, ShopDetailBean> detailModelHashMap = implModel.getGoodsMap().get(userId);
        if (detailModelHashMap == null) {
            return;
        }

        if (detailModelHashMap.containsKey(model.getGoods_id())) {
            //如果有，数量减1
            ShopDetailBean old = detailModelHashMap.get(model.getGoods_id());
            old.setGood_count(old.getGood_count() - 1);
            detailModelHashMap.put(model.getGoods_id(), old);
        }

        saveCartModel(implModel);
    }

    /**
     * 设置选中/不选中
     *
     * @param goodId
     */
    public static void selcetCart(String goodId, boolean select) {
        if (TextUtils.isEmpty(goodId)) {
            return;
        }
        CartImplModel implModel = getCartModel();
        if (implModel == null) {
            return;
        }
        if (implModel.getGoodsMap() == null) {
            return;
        }
        String userId = UserImpl.getUserId();
        if (TextUtils.isEmpty(userId)) {
            return;
        }

        HashMap<String, ShopDetailBean> detailModelHashMap = implModel.getGoodsMap().get(userId);
        if (detailModelHashMap == null) {
            return;
        }

        if (detailModelHashMap.containsKey(goodId)) {
            //如果有，数量减1
            ShopDetailBean old = detailModelHashMap.get(goodId);
            old.setSelect(select);
            detailModelHashMap.put(goodId, old);
        }

        saveCartModel(implModel);
    }

    /**
     * 从购物车删除
     *
     * @param goods_id
     */
    public static void deleteCart(String goods_id) {
        CartImplModel implModel = getCartModel();

        String userId = UserImpl.getUserId();
        if (TextUtils.isEmpty(userId)) {
            return;
        }
        if (implModel == null) {
            return;
        }

        HashMap<String, ShopDetailBean> detailModelHashMap = implModel.getGoodsMap().get(userId);
        if (detailModelHashMap == null) {
            return;
        }


        if (implModel != null && implModel.getGoodsMap() != null) {
            if (detailModelHashMap.containsKey(goods_id)) {
                detailModelHashMap.remove(goods_id);
            }
        }
        saveCartModel(implModel);
    }

    /**
     * 清空购物车
     */
    public static void clearCart() {
        CartImplModel implModel = getCartModel();
        if (implModel == null) {
            return;
        }

        String userId = UserImpl.getUserId();
        if (TextUtils.isEmpty(userId)) {
            return;
        }
        implModel.getGoodsMap().put(userId, null);

        saveCartModel(implModel);
    }


    public static class CartImplModel implements Serializable {

        private HashMap<String, HashMap<String, ShopDetailBean>> goodsMap;

        private long lastEditTime;

        public HashMap<String, HashMap<String, ShopDetailBean>> getGoodsMap() {
            return goodsMap;
        }

        public void setGoodsMap(HashMap<String, HashMap<String, ShopDetailBean>> goodsMap) {
            this.goodsMap = goodsMap;
        }

        public long getLastEditTime() {
            return lastEditTime;
        }

        public void setLastEditTime(long lastEditTime) {
            this.lastEditTime = lastEditTime;
        }
    }

}
